package itsm.isperp.module.service.db;

import itsm.isperp.framework.collection.map.LowercaseKeyHashMap;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.entity.db.DbEntityFieldControl;
import itsm.isperp.module.repository.db.DbEntityFieldControlMapper;
import itsm.isperp.module.repository.db.DbEntityMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 实体字段的控件定义服务
 * 
 * @author lizx
 * @since V1.0
 * 
 */
@Service
public class DbEntityFieldControlService extends
		BaseService<DbEntityFieldControl> {

	@Autowired
	protected DbEntityFieldControlMapper dbEntityFieldControlMapper;

	@Autowired
	protected DbEntityMapper dbEntityMapper;

	@Autowired
	protected JdbcTemplate jdbcTemplate;

	@Override
	public DbEntityFieldControlMapper getRepository() {
		return this.dbEntityFieldControlMapper;
	}

	/**
	 * 根据实体字段删除其控件
	 * 
	 * @param fieldId
	 */
	public void deleteByFieldId(String fieldId) {
		this.getRepository().deleteByFieldId(fieldId);
	}

	/**
	 * 根据实体id删除
	 * 
	 * @param id实体id
	 */
	public void deleteByEntityId(String entityId) {
		this.getRepository().deleteByEntityId(entityId);

	}

	/**
	 * 查询实体中的字段控件
	 * 
	 * @param request
	 *            数据请求
	 * @return 请求结果
	 */
	public DataResponse<Map<String, Object>> findOnEntityByRequest(
			DataRequest request) {
		return this.getRepository().findOnEntityByRequest(request);
	}

	/**
	 * 获取实体字段的控件定义
	 * 
	 * @param id
	 * @return
	 */
	public DbEntityFieldControl findByFieldId(String dbFieldid) {
		return this.getRepository().findByFieldId(dbFieldid);
	}

	/**
	 * 根据表名称及列名称取得实体空间定义
	 * 
	 * @param tableName
	 * @param columnName
	 * @return
	 */
	public DbEntityFieldControl findByTableAndColumn(String tableName,
			String columnName) {
		return this.getRepository().findByTableAndColumn(tableName, columnName);
	}

	/**
	 * 
	 * @param control
	 */
	public List<Map<String, Object>> getDatasource(DbEntityFieldControl control) {

		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(this
				.getDatasourceSql(control));
		List<Map<String, Object>> re = new ArrayList<Map<String, Object>>();

		for (Map<String, Object> m : list) {
			LowercaseKeyHashMap<String, Object> s = new LowercaseKeyHashMap<>();
			s.putAll(m);

			re.add(s);
		}

		return re;
	}

	public String getDatasourceSql(DbEntityFieldControl control) {

		DbEntity entity = this.dbEntityMapper.findOne(control
				.getDatasourceEntityId());

		if (entity == null) {
			return null;
		}

		StringBuffer sb = new StringBuffer("SELECT ");
		sb.append(control.getValueField());
		if (!control.getValueField().equals(control.getDisplayField())) {
			sb.append(",").append(control.getDisplayField());
		}

		sb.append(" FROM ")
				.append(entity.getTableName())
				.append(" WHERE 1=1 AND ")
				.append(StringUtils.isEmpty(control.getFilter()) ? " 1=1 "
						: control.getFilter());

		return sb.toString();
	}

	public List<Map<String, Object>> listByEntityId(String entityId,
			boolean isSlmField) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("entity_id", JqGridFilterRuleOp.eq, entityId);

		if (isSlmField) {
			req.addFilterRule("slm_calculate_field", JqGridFilterRuleOp.eq, "1");

		}

		return findOnEntityByRequest(req);
	}

}